#define _BSD_SOURCE  1
//#define _XOPEN_SOURCE  1
//#define _SVID_SOURCE  1
//#define _POSIX_SOURCE  1
#include <header.h>

int dirs = 0, files = 0;

int thisOrFather(char *path){
	return strcmp(path, ".") == 0 || strcmp(path, "..") == 0;
}

void tree(char *path, int deeps){
	DIR *dirp = opendir(path);
	ERROR_CHECK(dirp, NULL, "opendir");
	int ret1 = chdir(path);
	ERROR_CHECK(ret1, -1, "chdir");

	struct dirent *pdirent;
	while((pdirent = readdir(dirp)) != NULL){
		
		if(thisOrFather(pdirent->d_name)){
			continue;
		}

		printf("└");
		int i;
		for (i = 0; i < deeps * 3; i++){
			printf("─");
		}
		
		printf("%s\n", pdirent->d_name);
		if(pdirent->d_type == DT_DIR ){
			dirs++;
			tree(pdirent->d_name, deeps + 1);
		}
		files++;
	}
	int ret2 = chdir("..");
	ERROR_CHECK(ret2, -1, "chdir");

	closedir(dirp);
}



int main(int argc, char* argv[]){

	if(argc == 1){
		tree(".", 1);
	}else if(argc == 2){

		tree(argv[1], 1);
	}else{
		perror("argv");
		exit(-1);
	}
	printf("files: %d , directoris : %d\n",files,dirs);
	
	return 0;
}